var href = location.search;
var tag;
var prefix = href.substring(1,href.indexOf("="));
var id = href.substring(href.indexOf("=")+1);

if(prefix === "primaryClassification"){
    // 一级分类小说列表
    url = api+"sort/primaryClassification/"+id+"?page=1";
    tag = sort(id);
}else if (prefix === "secondaryClassification"){
    // 二级分类小说列表
    url = api+"sort/secondaryClassification/"+id+"?page=1";
    tag = sort(id);
}else if (prefix === "tertiaryClassification") {
    //三级分类小说列表
    url = api+"sort/tertiaryClassification/"+id+"?page=1";
    tag = decodeURI(id);
}else if(prefix === "keyword"){
    url = api+"sort/search?keyword="+id;
    tag = decodeURI(id);
}else {
    url = api+"sort/author/"+id+"?page=1";
    tag = decodeURI(id);
}
$(function () {
    pageTurning(1);
});
function pageTurning(pageNum) {
    if(prefix != "keyword"){

        url = url.substring(0,url.lastIndexOf("="))+"="+pageNum;
    }
    var html = ""
    $.getJSON(url,function (data) {
        for (var i = 0; i < data.object.length; i++) {
            html = html + '<dl class="plist">' +
                '<dt>' +
                '<a href="/Novel-Website/page/目录.html?id='+data.object[i].id+'">'+data.object[i].bookName+'</a></dt>\n' +
                '                    <dd class="des">'+data.object[i].introduction+' </dd>\n' +
                '                    <dd>\n' +
                '                        分类： <a href="/Novel-Website/page/sort.html?secondaryClassification='+data.object[i].secondaryClassification+'">'+classification(data.object[i].secondaryClassification)+'</a>\n' +
                '                        &nbsp; <a href="/Novel-Website/page/sort.html?tertiaryClassification='+data.object[i].tertiaryClassification+'">'+data.object[i].tertiaryClassification+'&nbsp;&nbsp;&nbsp;&nbsp;\n <a href="/Novel-Website/page/sort.html?author='+data.object[i].author+'" >'+data.object[i].author+'</a>'+
                '                        <div align="right"><a href="/Novel-Website/page/目录.html?id='+data.object[i].id +'" class="vw">查看全文</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n' +
                '                        </div>\n' +
                '                        <br>\n' +
                '\n' +
                '                    </dd>\n' +
                '                </dl>'
        }
        document.getElementById("main").innerHTML = html;
        document.getElementById("ptop").innerHTML = ' <a href="/Novel-Website/page/主页.html">首页</a>&nbsp;&gt;&nbsp;\n' +
            '                    <a  href='+location.search+'>\n' +
            tag+
            '                    </a>';
        /*
        *1.一共展示10个页码，能够达到前5后4的效果
        * 2.如果前边不够5个，后边补齐10个
        * 3.如果后边不足4个，前边补齐10个
        * */
        // 定义开始位置begin,结束位置 end
        var begin; // 开始位置
        var end ; //  结束位置
        //1.要显示10个页码
        if(data.totalPage < 10){
            //总页码不够10页
            begin = 1;
            end = data.totalPage;
        }else {
            //总页码超过10页
            begin = data.currentPage - 5;
            end = data.currentPage + 4;
        }
        //2.如果前边不够5个，后边补齐10个
        if(begin < 1){
            begin = 1;
            end = begin + 9;
        }
        //3.如果后边不足4个，前边补齐10个
        if(end > data.totalPage){
            end = data.totalPage;
            begin = end - 9 ;
        }
        var lis = "";
        var beginPage = '<a href="javascript:pageTurning(1)" title="首页">«</a>';
        lis += beginPage;
        for (var i = begin; i <= end ; i++) {
            var li;
            //判断当前页码是否等于i
            if(data.currentPage == i){

                li = '<span>'+data.currentPage+'</span>'

            }else{
                //创建页码的li
                li = ' <a href="javascript:pageTurning('+i+')">'+i+'</a>';
            }
            //拼接字符串
            lis += li;
        }
        var endPage = ' <a href="javascript:pageTurning('+data.totalPage+')" title="尾页">»</a>';
        lis += endPage;
        //将lis内容设置到 ul
        $("#pagenavi").html(lis);
        getJson(api+"recommend/getNewest","getNewest");
        getJson(api+"recommend/getTotalClicks","getTotal");
        getJson(api+"recommend/getMonthClicks","getMonth");
        getJson(api+"recommend/getRandom","getRandom");
        //定位到页面顶部
        window.scrollTo(0,0);
    })
}





function classification(data){
    var classificationName;
    switch (data) {
        case 1:
            classificationName = "都市·娱乐";
            break;
        case 7:
            classificationName = "二次元";
            break;
        case 8:
            classificationName = "精校武侠";
            break;
        case 9:
            classificationName = "精校仙侠";
            break;
        case 10:
            classificationName = "精校奇幻";
            break;
        case 11:
            classificationName = "精校玄幻";
            break;
        case 12:
            classificationName = "精校科幻";
            break;
        case 13:
            classificationName = "精校灵异";
            break;
        case 14:
            classificationName = "精校历史";
            break;
        case 15:
            classificationName = "精校军事";
            break;
        case 16:
            classificationName = "精校竞技";
            break;
        case 17:
            classificationName = "精校游戏";
            break;
        default:
            break;
    }
    return classificationName;
}
function sort(data){
    var classificationName;
    switch (data) {
        case "1":
            classificationName = "都市·娱乐";
            break;
        case "2":
            classificationName = "武侠·仙侠";
            break;
        case "3":
            classificationName = "奇幻·玄幻";
            break;
        case "4":
            classificationName = "科幻·灵异";
            break;
        case "5":
            classificationName = "历史·军事";
            break;
        case "6":
            classificationName = "竞技·游戏";
            break;
        case "7":
            classificationName = "二次元";
            break;
        case "8":
            classificationName = "精校武侠";
            break;
        case "9":
            classificationName = "精校仙侠";
            break;
        case "10":
            classificationName = "精校奇幻";
            break;
        case "11":
            classificationName = "精校玄幻";
            break;
        case "12":
            classificationName = "精校科幻";
            break;
        case "13":
            classificationName = "精校灵异";
            break;
        case "14":
            classificationName = "精校历史";
            break;
        case "15":
            classificationName = "精校军事";
            break;
        case "16":
            classificationName = "精校竞技";
            break;
        case "17":
            classificationName = "精校游戏";
            break;
        default:
            break;
    }
    return classificationName;
};
function getJson(url,id) {
    $.getJSON(url, function (data) {
        var html = "";
        if (data.code == 200){
            for (var i = 0; i < 8; i++) {
                html = html + ' <li>' +
                    '<a href="/Novel-Website/page/目录.html?id='+data.data[i].id+'" title="'+ data.data[i].bookName + data.data[i].author+'">'
                    + data.data[i].bookName + data.data[i].author +
                    '</a>' +
                    '<spanclass = "updatetime">'+data.data[i].saveTime+'</span>' +
                    '</li>';
            }
        }
        document.getElementById(id).innerHTML = html;
    })
};